package com.springbootinit.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springbootinit.common.BaseResponse;
import com.springbootinit.common.ErrorCode;
import com.springbootinit.common.ResultUtils;
import com.springbootinit.datasource.DataSource;
import com.springbootinit.datasource.PictureDataSource;
import com.springbootinit.datasource.PostDataSource;
import com.springbootinit.datasource.UserDataSource;
import com.springbootinit.exception.ThrowUtils;
import com.springbootinit.model.dto.search.SearchRequest;
import com.springbootinit.model.enums.SearchTypeEnum;
import com.springbootinit.model.vo.PostVO;
import com.springbootinit.model.vo.SearchVO;
import com.springbootinit.model.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/search")
@Slf4j
public class SearchController {

    @Resource
    private PictureDataSource pictureDataSource;

    @Resource
    private PostDataSource postDataSource;

    @Resource
    private UserDataSource userDataSource;

    private final Map<String, DataSource> typeDataSourceMap = new HashMap<>();

    @PostConstruct
    public void init(){
        typeDataSourceMap.put(SearchTypeEnum.POST.getValue(), postDataSource);
        typeDataSourceMap.put(SearchTypeEnum.USER.getValue(), userDataSource);
        typeDataSourceMap.put(SearchTypeEnum.PICTURE.getValue(), pictureDataSource);
    }
    @PostMapping("/all")
    public BaseResponse<SearchVO> searchAll(@RequestBody SearchRequest searchRequest, HttpServletRequest request) {
        String type = searchRequest.getType();
        SearchTypeEnum searchTypeEnum = SearchTypeEnum.getEnumByValue(type);
        String searchText = searchRequest.getSearchText();
        long current = searchRequest.getCurrent();
        long pageSize = searchRequest.getPageSize();
        ThrowUtils.throwIf(StringUtils.isBlank(type), ErrorCode.PARAMS_ERROR);
        if (searchTypeEnum == null) {
            Page<UserVO> userVOPage = userDataSource.doSearch(searchText, current, pageSize);
            Page<PostVO> postVOPage = postDataSource.doSearch(searchText, current, pageSize);
//            Page<Picture> picturePage = pictureDataSource.doSearch(searchText, current, pageSize);

            SearchVO searchVO = new SearchVO();
            searchVO.setPostList(postVOPage.getRecords());
//        searchVO.setPictureList(picturePage.getRecords());
            searchVO.setUserList(userVOPage.getRecords());
            return ResultUtils.success(searchVO);
        } else {
            SearchVO searchVO = new SearchVO();
            DataSource dataSource = typeDataSourceMap.get(type);
            Page<?> dataList = dataSource.doSearch(searchText, current, pageSize);
            searchVO.setDataList(dataList.getRecords());
            return ResultUtils.success(searchVO);
        }
    }
}
